#!/usr/bin/env python

"""
This sample script reads all the programming information from the
data store and prints the output to STDOUT.

It can easily be adapted to suit your needs.
"""

import shelve, xml.dom.minidom, time, os, os.path, urllib,re, sys, BitTorrent.bencode 
from configuration import *

infoStore = shelve.open(os.path.expanduser(infoStoreFilename))

# Iterate through all items in the store...
for title, lastSeen in infoStore.iteritems():
  try:
    # .. skipping those which aren't programme information.
    if not title.startswith("Prog: "): continue
    # Get rid of the "Prog: " which precedes the programme title
    title = title[6:]
    # Print the title
    print "%s: " % title,
    # Check whether we have a record of the season/episode, or whether we just 
    # have a date.
    if isinstance(lastSeen, tuple):
        print "Season %d, episode %d" % lastSeen
    elif isinstance(lastSeen, set):
            print "Set: %s" % str(lastSeen)

    else:
        lastSeenDate = int(lastSeen)
        year = lastSeenDate / 10000
        month = (lastSeenDate / 100) % 100
        day = lastSeenDate % 100
        # DD-MM-YYYY
        print "%02d-%02d-%04d" % (day, month, year)
  except Exception, ex:
    print ex
